# -*- coding: utf-8 -*-
# @name             : base64转换
# @author           : OneYoung
# @create           : 2024/07/18
# @version          : v1.0
# @desc             :

import base64


# 字符串转base64
def encode_base64(string):
    decoded = base64.b64encode(string.encode('utf-8'))
    return decoded


# base64解码
def decode_base64(b64_str):
    string = base64.b64decode(b64_str)
    return str(string, 'utf-8')


# base64字符串转字节
def base64_to_bytes(base64_str):
    context = base64_str.split(",")[-1]
    decode_bytes = base64.b64decode(context)
    return decode_bytes


# 文件转base64字符串
def file_encode_base64(file):
    with open(file, 'rb') as f:
        bytes_file = f.read()
    encode = base64.b64encode(bytes_file)
    base64_str = encode.decode()
    return base64_str
